package de.revdev.llt.bean.managed;

import de.revdev.llt.bean.entities.Credential;
import de.revdev.llt.bean.entities.PrivateClassRoom;
import de.revdev.llt.bean.entities.User;
import de.revdev.llt.bean.facade.UserFacade;
import de.revdev.llt.bean.utility.MessageProvider;
import de.revdev.llt.security.utility.Password;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author f.lindermaier
 */
@Named(value="registrationBean")
@RequestScoped
public class RegistrationBean {

    private UserFacade uf;
    private MessageProvider provider;
    private static final Logger log = Logger.getLogger(RegistrationBean.class.getName());
    private User user;
    private Credential credential;
    private UIComponent emailField;

    /**
     * Creates a new instance of RegistrationBean
     */
    public RegistrationBean() {
    }

    @PostConstruct
    private void init() {
        user = new User();
        credential = new Credential();
    }

    public String register() {
        try {
            uf.findByEmail(user.getEmail());
            provider.addMessage(emailField.getClientId(), FacesMessage.SEVERITY_ERROR, "registrationBean_email_exists");
            log.log(Level.INFO, "Registration fehlgeschlagen, die EMail-Adresse {0} ist schon vorhanden", user.getEmail());
            return "";
        } catch (Exception e) {
            try {
                Password pw = new Password();
                byte[] saltBytes = pw.getSalt(64);
                credential.setSalt(pw.base64FromBytes(saltBytes));
                credential.setPassword(pw.hashWithoutSalt(credential.getPassword()));
                credential.setRole("user");
                user.setCredential(credential);
                user.setPrivateClassRoom(new PrivateClassRoom());
                uf.create(user);
                return "/faces/index.xhtml?faces-redirect=true";
            } catch (Exception ee) {
                provider.addMessage(null, FacesMessage.SEVERITY_FATAL, "registrationBean_internal_error");
                log.log(Level.SEVERE, "Es ist ein Fehler beim Registrieren eines neuen Users aufgetreten", ee);
                return "";
            }
        }
    }

    @Inject
    public void setMessageProvider(MessageProvider messageProvider) {
        this.provider = messageProvider;
    }

    @Inject
    public void setUserFacade(UserFacade userFacade) {
        this.uf = userFacade;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Credential getCredential() {
        return credential;
    }

    public void setCredential(Credential credential) {
        this.credential = credential;
    }

    public UIComponent getEmailField() {
        return emailField;
    }

    public void setEmailField(UIComponent emailField) {
        this.emailField = emailField;
    }
}